package com.x.mygulimall5.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.x.mygulimall5.admin.service.UserService;
import com.x.mygulimall5.common.entity.admin.UserEntity;
import com.x.mygulimall5.common.utils.R;
import com.x.mygulimall5.common.vo.admin.UserQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/user")
@CrossOrigin
public class UserController {

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/login")
    public R login() {
        return R.ok().data("token","admin");//token为admin
    }

    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/info")
    public R info() {
        return R.ok().data("roles","[admin]").data("name","admin").data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }

    @Autowired
    private UserService userService;
//分页查询
    //current 当前第几页
    //limit  每页显示几条数据

    @GetMapping("/userPage/{current}/{limit}")
    public R userPage(
            @PathVariable Long current,
            @PathVariable Long limit){
        //创page对象
        Page<UserEntity> pageResult = new Page<>(current,limit);
        System.out.println(current);
        //调用方法实现分页
        userService.page(pageResult,null);
        long total = pageResult.getTotal();//总记录数
        List<UserEntity> records = pageResult.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    //条件查询带分页方法
    @PostMapping("/pageUserCondition/{current}/{limit}")
    public R pageUserCondition(
            @PathVariable Long current,
            @PathVariable Long limit,
            @RequestBody(required = false) UserQueryVO userQueryVO){
        //创page对象
        Page<UserEntity> pageUser = new Page<>(current,limit);

        //构建条件
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //类比mybatis学的动态sql
        String username = userQueryVO.getUsername();
        //Integer level = teacherQuery.getLevel();
        String begin = userQueryVO.getBegin();
        String end = userQueryVO.getEnd();
        //判断条件是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(username)){
            wrapper.like("username",username);
        }
        /*if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }*/
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现分页
        userService.page(pageUser,wrapper);

        long total = pageUser.getTotal();//总记录数
        List<UserEntity> records = pageUser.getRecords();//数据list集合

        return R.ok().data("total",total).data("rows",records);
    }

}
